export async function categoryAction ({ request }) {
  const formData = await request.formData()
  const data = Object.fromEntries(formData)

  switch (request.method) {
    case 'POST': {
      await window.api.sql(
        'insert into categories (name, created_at) values(\'未命名分类\', datetime())',
        'insert',
      )
      return {}
    }
    case 'DELETE': {
      const { id } = data

      await window.api.sql(
        'delete from categories where id=@id',
        'del',
        {
          id,
        },
      )
      await window.api.sql(
        'update contents set category_id=0 where category_id=@id',
        'update',
        {
          id,
        },
      )
      return {}
    }
    case 'PATCH': {
      await window.api.sql(
        'update categories set name=@name where id=@id',
        'update',
        data,
      )
    }
  }
  return {}
}
